function exportExcelCsv(title, data, fileName) {
  let tables = ''
  let row = ''
  const typeArray = {
    xls: 'application/vnd.ms-excel',
    xlsx: 'application/vnd.ms-excel',
    csv: 'text/csv'
  }
  for (var item in title) {
    if (title[item]) {
      row += title[item] + ','
    }
  }
  row = row.slice(0, -1)
  tables += row + '\r\n'
  for (let i = 0; i < data.length; i++) {
    let row = ''
    for (var j in title) {
      if (title[j]) {
        if (data[i][j]) {
          // 先变成字符串，如果有特殊符号，就要替换
          let str = String(data[i][j])
          str = str.replace(',', '，')
          // 如果是数字，就要加个'
          if (!isNaN(str) && str > 10000000000) {
            str += '`'
          }
          row += `${str},`
        } else {
          row += ','
        }
      }
    }
    row.slice(0, row.length - 1)
    tables += row + '\r\n'
  }
  if (tables === '') {
    alert('Invalid data')
    return
  }
  const fileType = fileName.split('.')
  const uri = new Blob(['\ufeff' + tables], { type: typeArray[fileType[1]] })
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    // for IE
    window.navigator.msSaveOrOpenBlob(tables, fileName)
  } else {
    // for Non-IE（chrome、firefox etc.）
    const link = document.createElement('a')
    link.href = URL.createObjectURL(uri)
    link.style = 'visibility:hidden'
    link.download = fileName
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }
}

export default exportExcelCsv
